package com.cjm.one.goods.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.result.Result;
import com.cjm.one.goods.service.GoodsSpecService;
import com.cjm.one.model.goods.entity.GoodsSpec;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "商品规格管理")
@RequestMapping("/admin/goods/spec")
public class GoodsSpecController
{
    @Resource
    private GoodsSpecService goodsSpecService;
    @GetMapping("list")
    @Operation(summary = "获取商品规格列表")
    public Result<Page<GoodsSpec>> list(@RequestParam(name = "current") Long current,
                                        @RequestParam(name = "size") Long size,
                                        @RequestParam(name = "name",required = false) String specName){
        return Result.ok(goodsSpecService.pageList(current,size,specName));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id获取商品规格")
    public Result<GoodsSpec> getById(@PathVariable(name = "id") Long id){
        return Result.ok(goodsSpecService.getById(id));
    }
    @DeleteMapping("/{id}")
    @Operation(summary = "根据id删除商品规格")
    public Result<Boolean> delete(@PathVariable(name = "id") Long id){
        boolean b = goodsSpecService.removeById(id);
        return Result.ok(b);
    }
    @PutMapping
    @Operation(summary = "更新商品规格")
    public Result<Boolean> update(@RequestBody GoodsSpec goodsSpec){
        boolean b = goodsSpecService.updateById(goodsSpec);
        return Result.ok(b);
    }

    @PostMapping
    @Operation(summary = "添加商品规格")
    public Result<Boolean> add(@RequestBody GoodsSpec goodsSpec){
        boolean b = goodsSpecService.save(goodsSpec);
        return Result.ok(b);
    }
    @GetMapping("/all")
    @Operation(summary = "获取所有商品规格")
    public Result<List<GoodsSpec>> all(){
        return Result.ok(goodsSpecService.getAll());
    }
}
